package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.hz.Zdy;
import com.example.mapper.ZdyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin // 跨域
@RestController
@RequestMapping("/api/Zdy")
@Api(tags = "自定义表格")
public class ZdyController {

    @Resource
    private ZdyMapper zdyMapper;

    @GetMapping("/queryOne/{code}")
    @ApiOperation("通过登录人code查询自定义表格信息")
    public Result queryOne(@PathVariable String code){
        Zdy zdy = this.zdyMapper.selectOne(new LambdaQueryWrapper<Zdy>().eq(Zdy::getCode, code));
        if (zdy!=null){
            return Result.success(zdy.getContent());
        }
        return Result.success();
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改表格信息")
    @Log("新增或修改表格信息")
    public Result queryOne(@RequestBody Zdy zdy){
        try{
            Zdy zdy1 = this.zdyMapper.selectOne(new LambdaQueryWrapper<Zdy>().eq(Zdy::getCode, zdy.getCode()));
            if (zdy1!=null){
                zdy1.setContent(zdy.getContent());
                int update = this.zdyMapper.updateById(zdy1);
                if (update>0) return Result.success();
            }else {
                int insert = this.zdyMapper.insert(zdy);
                if (insert>0) return Result.success();
            }
            return Result.error("错误");
        }catch (Exception e){
            throw new YiFeiException(500,"新增或修改发生异常"+e.getMessage());
        }
    }

}
